//
//  ZYXNSThreadViewController.m
//  MultiThreading
//  NSThread
//

#import "ZYXNSThreadViewController.h"

@interface ZYXNSThreadViewController ()
@property (strong,nonatomic) NSThread *thread;
@end

@implementation ZYXNSThreadViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSThread mainThread] setName:@"应用运行主线程"];
    NSLog(@"viewDidLoad 执行线程是 %@",[NSThread currentThread]);
    
    
    //在主线程开启一个run方法执行的子线程
    //self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    //[self.thread setName:@"run方法执行子线程"];
    
}

/**
 *  文件下载模拟方法
 */
-(void)download:(NSString *)url
{
    NSLog(@"download 执行线程是 %@ 当前应用运行的主线程是 %@ 该方法%d主线程执行的",[NSThread currentThread],[NSThread mainThread],[NSThread isMainThread]);
    
    NSLog(@"从 %@ 下载资源",url);
    
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSThread *mainThread = [NSThread mainThread];
    mainThread.name = @"应用运行主线程";
    NSLog(@"-(void)touchesBegan:withEvent: 方法执行线程是 %@ 该方法%d主线程执行的",[NSThread currentThread],[NSThread isMainThread]);
    
    //[self test1];
    
    //[self test2];
    
    //[self test3];
    
    //[self test4];
    
    //[self test5];
    
    //在主线程启动run方法执行子线程
    //[self.thread start];
    //[self.thread start]; //错误一个子线程执行完毕就再内存中销毁了 再次使用会推出应用
    
    
    //子线程安全执行的写法
    [self safeThread];
    
}

-(void)test1
{
    //创建线程
    //NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download) object:nil];
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:nil];
    
    [thread setName:@"下载子线程"];
    
    //启动线程 调用self的download方法
    [thread start];
}

-(void)test2
{
    //创建线程后自动启动线程
    //detach 分离 从当前线程中分离出新线程 代表创建一条新线程
    [NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:nil];
}

-(void)test3
{
    //当前线程把参数 @"http://www.baidu.com/a.mp3" 传给新线程
    [NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:@"http://www.baidu.com/a.mp3"];
}

-(void)test4
{
    //相当于 [self download:@"http://www.baidu.com/zyx.mp3"] 并没有开启新的子线程
    //[self performSelector:@selector(download:) withObject:@"http://www.baidu.com/zyx.mp3"];
    
    //隐式创建并启动新的子线程
    [self performSelectorInBackground:@selector(download:) withObject:@"http://www.baidu.com/zyx.mp3"];
    
}


-(void)run
{
    NSLog(@"run 执行线程是 %@ 当前应用运行的主线程是 %@ 该方法%d主线程执行的",[NSThread currentThread],[NSThread mainThread],[NSThread isMainThread]);
    
    NSLog(@"run begin-------------");
    
    //睡眠5秒
    //[NSThread sleepForTimeInterval:5];                                                          //Block 线程阻塞状态
    
    //从现在开始3秒后执行子线程任务                                                                    //Block 线程阻塞状态
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:3];
    [NSThread sleepUntilDate:date];
    
    for(int i=0;i<10;i++){                                                                        //Runing 线程运行
        NSLog(@"%d",i);
        
        //if (i==4) return;                                                                       //Dead 任务中途停止执行
        if (i==4) [NSThread exit];
        
    }
    NSLog(@"run end-------------");                                                               //Dead 任务完成线程死亡
    
}

-(void)test5
{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; //new  创建线程
    thread.name = @"run执行子线程";
    [thread start];                                                                               //Runable 线程就绪可调度状态
}



-(void)safeThread
{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; //new  创建线程
    thread.name = @"run执行子线程";
    [thread start];                                                                               //Runable 线程就绪可调度状态
}








@end
